package com.movie.controller;

import com.movie.dto.ResponseDTO;
import com.movie.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @PostMapping("/add")
    public ResponseDTO<String> addFavorite(@RequestParam Integer movieId,
                                           @RequestHeader("X-User-Id") Integer userId) {
        try {
            favoriteService.addFavorite(userId, movieId);
            return ResponseDTO.success("添加收藏成功", "评分成功");
        } catch (Exception e) {
            return ResponseDTO.error("添加收藏失败");
        }
    }

    @PostMapping("/remove")
    public ResponseDTO<String> removeFavorite(@RequestParam Integer movieId,
                                              @RequestHeader("X-User-Id") Integer userId) {
        try {
            favoriteService.removeFavorite(userId, movieId);
            return ResponseDTO.success("取消收藏成功", "评分成功");
        } catch (Exception e) {
            return ResponseDTO.error("取消收藏失败");
        }
    }

    @GetMapping("/check")
    public ResponseDTO<Boolean> checkFavorite(@RequestParam Integer movieId,
                                              @RequestHeader("X-User-Id") Integer userId) {
        try {
            boolean isFavorite = favoriteService.isFavorite(userId, movieId);
            return ResponseDTO.success(isFavorite, "评分成功");
        } catch (Exception e) {
            return ResponseDTO.error("检查收藏状态失败");
        }
    }
}
